package com.swa99ylum.pxf;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.swa99ylum.pxf.cell.CellFiller;
import com.swa99ylum.pxf.row.RowFiller;
import com.swa99ylum.pxf.row.indexer.RowIndexer;

public class Test2 {

	
	public static void main(String[] args) throws Exception {
		
		
		File file = new File("test2.xlsx");
		System.out.println(file.getAbsolutePath());
		
		
		FileInputStream fis = new FileInputStream(file);
		XSSFWorkbook xwb = new XSSFWorkbook(fis);
		
		
		XSSFSheet sheet = xwb.getSheetAt(0);
		
		/** ===================================================== */
		
		/** 开发者 需要定义的 数据 */
		
		List<Student> sss = new ArrayList<>();
		sss.add(new Student(123, "张三", "上海", new Date(), "第一批"));
		sss.add(new Student(124, "李四", "北京", new Date(), "第一批"));
		sss.add(new Student(125, "王五", "四川", new Date(), "第二批"));
		sss.add(new Student(126, "赵六", "西安", new Date(), "第一批"));
		
		RowFiller rowFiller = new RowFiller(7, sss, new CellFiller[] {
				new CellFiller(0, new Object[]{"@{batch}", "@{id}", "@{name}", "@{address}", "@{birthDay}"}),
				new CellFiller(8, new Object[]{"@{address}", "@{birthDay}"}),
		});
				
		/** ===================================================== */
		
		/** 模拟了 SheetFiller 的 fill() 方法 */
		
		List<XSSFRow> rowList = new ArrayList<>();//待 填值
		
		List<?> dataList = rowFiller.getDataList();	//规定了 高度，是下面 循环的 标准
		RowIndexer rowIndexer = rowFiller.getRowIndexer();
		
		/*
		 * 设置 rowFiller 的 rows
		 */
		for (Object data : dataList) {
			
			//1、设置 rowList
			int rowIndex = rowIndexer.getIndexValue();
			
			XSSFRow row = sheet.getRow(rowIndex);
			if(row == null) row = sheet.createRow(rowIndex);
			rowList.add(row);
			
			rowIndexer.incrementIndexValue();
		}
		
		XSSFRow[] rows = (XSSFRow[]) rowList.toArray(new XSSFRow[0]);
		rowFiller.setRows(rows);
		
		rowFiller.fill();
		
		/** ===================================================== */
		
		FileOutputStream fo = new FileOutputStream(file);
		xwb.write(fo);
		fo.close();
		
	}
}
